public with sharing class Cald_DisputeDetailVF {
	public Callidus_Dispute__c cdObj {get; set;}
	public Callidus_Dispute__c disputeObj {get; set;}
	public Boolean showApproveBut {get; set;}
	public Boolean showRejectBut {get; set;}
	public Boolean showWithdrawBut {get; set;}
	public Boolean showReadOnlyFlag {get; set;}
	public Boolean showInquiryBut {get; set;}
	public Boolean showEditFlag {get; set;}
	
	public List<Opportunity> opportunityList {get; set;}
	
	public Cald_DisputeDetailVF(ApexPages.StandardController myController)
	{
		cdObj = (Callidus_Dispute__c)myController.getRecord();
		showInquiryBut = showWithdrawBut = showEditFlag = showApproveBut = showRejectBut = false;
		disputeObj = [select Id, Name, Claim__c, Opportunity__c, Filing_Date__c, Status__c, Dispute_Reasons__r.Description__c, Amount__c, Comments__c, Assigned_To__c, OwnerId, Level1_Approver__c, Level2_Approver__c, Level3_Approver__c, Level4_Approver__c, Level5_Approver__c, Level6_Approver__c, Final_Approver__c, CreatedById from Callidus_Dispute__c where id = :cdObj.id];
		
		if(disputeObj.Assigned_To__c == UserInfo.getUserId())
		{
			showEditFlag = showApproveBut = showRejectBut = true;
			if(disputeObj.Status__c == 'Pending Approval')
				showInquiryBut = true;
		}
		if(disputeObj.ownerId == UserInfo.getUserId())
		{
			showReadOnlyFlag = true;
			showEditFlag = false;
			//if(disputeObj.Status__c == 'Pending Approval' || disputeObj.Status__c == 'Approved' || disputeObj.Status__c == 'Rejected')
			if(disputeObj.Status__c != 'Withdrawn')
				showWithdrawBut = true;			
		}
		
		Profile[] p = [select Name from Profile where id = :UserInfo.getProfileId()];
		if(p.size() > 0 && p[0].Name == 'System Administrator')
			showWithdrawBut = true;
		
	}
	
    public List<Opportunity> getOpportunityDetails()
    {
    	opportunityList = new List<Opportunity>();
    	if(disputeObj.Opportunity__c != null)
        	opportunityList = [select Name, Amount, AccountId, Opportunity.Account.Name, ExpectedRevenue, Type, Description, OwnerId, Opportunity.Owner.Name, CloseDate, LeadSource, Opportunity.Campaign.Name, MainCompetitors__c, OrderNumber__c, CurrentGenerators__c, TrackingNumber__c from Opportunity where id = :disputeObj.Opportunity__c];
        if(opportunityList.size() == 0)
        	opportunityList = null;
        return opportunityList;
    }
    
    public PageReference approve()
    {
        try
        {
            List<User> managerList = [select ManagerId from User where id = :UserInfo.getUserId()];
            
            if(disputeObj.Level1_Approver__c == disputeObj.Assigned_To__c && disputeObj.Level2_Approver__c != null && managerList.size() > 0 && managerList[0].managerId != null)
            {
                disputeObj.Assigned_To__c = managerList[0].managerId;
            }
            else if(disputeObj.Level2_Approver__c == disputeObj.Assigned_To__c && disputeObj.Level3_Approver__c != null && managerList.size() > 0 && managerList[0].managerId != null)
            {
                disputeObj.Assigned_To__c = managerList[0].managerId;
            }
            else if(disputeObj.Level3_Approver__c == disputeObj.Assigned_To__c && disputeObj.Level4_Approver__c != null && managerList.size() > 0 && managerList[0].managerId != null)
            {
                disputeObj.Assigned_To__c = managerList[0].managerId;
            }
            else if(disputeObj.Level4_Approver__c == disputeObj.Assigned_To__c && disputeObj.Level5_Approver__c != null && managerList.size() > 0 && managerList[0].managerId != null)
            {
                disputeObj.Assigned_To__c = managerList[0].managerId;
            }
            else if(disputeObj.Level5_Approver__c == disputeObj.Assigned_To__c && disputeObj.Level6_Approver__c != null && managerList.size() > 0 && managerList[0].managerId != null)
            {
                disputeObj.Assigned_To__c = managerList[0].managerId;
            }
            else if(disputeObj.Level6_Approver__c == disputeObj.Assigned_To__c && disputeObj.Level7_Approver__c != null && managerList.size() > 0 && managerList[0].managerId != null)
            {
                disputeObj.Assigned_To__c = managerList[0].managerId;
            }
            else if(disputeObj.Final_Approver__c != null)
            {
                disputeObj.Assigned_To__c = disputeObj.Final_Approver__c;
            }
            else
                disputeObj.Assigned_To__c = null;
            
        	disputeObj.Status__c = 'Approved';
        	update disputeObj;
			showReadOnlyFlag = true;
			showInquiryBut = showEditFlag = false;
        	
            ProcessInstance[] prInst = [Select Id from ProcessInstance where TargetObjectId=:disputeObj.Id and Status='Pending'];
            if(prInst.size()>0)     
            {
                ProcessInstanceWorkitem[] prInstWItem = [select Id,OriginalActorId from ProcessInstanceWorkitem where ProcessInstanceId = :prInst[0].Id]; 
    
                Approval.ProcessWorkitemRequest prWkItem = new Approval.ProcessWorkitemRequest();
    
                prWkItem.setWorkitemId(prInstWItem[0].Id);
    
                prWkItem.setComments(disputeObj.Comments__c); 
                
                prWkItem.setAction('Approve'); 

                Approval.ProcessResult prResult = Approval.process(prWkItem);
                
            }
        }
        catch(Exception e)
        {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.info, e.getMessage()));
        }
        return null;
    }

    public PageReference reject()
    {
        try
        {
        	disputeObj.Status__c = 'Rejected';
        	update disputeObj;
			showReadOnlyFlag = true;
			showInquiryBut = showEditFlag = false;
        	
            ProcessInstance[] prInst = [Select Id from ProcessInstance where TargetObjectId=:disputeObj.Id and Status='Pending'];
            if(prInst.size()>0)     
            {
                ProcessInstanceWorkitem[] prInstWItem = [select Id,OriginalActorId from ProcessInstanceWorkitem where ProcessInstanceId = :prInst[0].Id]; 
    
                Approval.ProcessWorkitemRequest prWkItem = new Approval.ProcessWorkitemRequest();
    
                prWkItem.setWorkitemId(prInstWItem[0].Id);
    
                prWkItem.setComments(disputeObj.Comments__c); 
                
                prWkItem.setAction('Reject'); 

                Approval.ProcessResult prResult = Approval.process(prWkItem);
            }
        }
        catch(Exception e)
        {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.info, e.getMessage()));
        }
        return null;
    }
    
    public PageReference withdraw()
    {
    	PageReference p = null;
        try
        {
	    	ProcessInstance[] prInst = [Select Id from ProcessInstance where TargetObjectId=:disputeObj.Id and Status='Pending'];
	        if(prInst.size()>0)     
	        {
	    	
	            ProcessInstanceWorkitem[] prInstWItem = [select Id,OriginalActorId from ProcessInstanceWorkitem where ProcessInstanceId = :prInst[0].Id]; 
	    
	            Approval.ProcessWorkitemRequest prWkItem = new Approval.ProcessWorkitemRequest();
	    
	            prWkItem.setWorkitemId(prInstWItem[0].Id);
	    
	            prWkItem.setComments('Withdrawn'); 
	                
	            prWkItem.setAction('Removed'); 
	
	            Approval.ProcessResult prResult = Approval.process(prWkItem);
	        }
            	
			if(disputeObj.Status__c != 'Approved')
            {
                disputeObj.Status__c = 'Withdrawn';
                disputeObj.Assigned_To__c = null;
                update disputeObj;
            }
            
            Callidus_Dispute__c newDispute = new Callidus_Dispute__c(Status__c = 'Draft', Claim__c = disputeObj.Claim__c, Opportunity__c = disputeObj.Opportunity__c, ownerId= disputeObj.ownerId);
            
            insert newDispute;
            p = new PageReference('/apex/Cald_NewDispute?id='+newDispute.id);
            p.setRedirect(true);
			            
        }
        catch(Exception e)
        {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.info, e.getMessage()));
        }
        return p;
    }//end of withdraw()
    
	public PageReference sendInquiry()
	{
		return null;
	}
}